<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form
              ref="pwdForm"
              :rules="rules"
              :model="pwdInfo"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="pwdInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="pwdInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleUpdate">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link :to="`/employees/print/${userId}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <!-- 内容 -->
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link :to="`/employees/print/${userId}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <!-- 内容 -->
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import JobInfo from './components/JobInfo.vue'
import UserInfo from './components/UserInfo.vue'
import { getDetailInfo } from '@/api/user'
import { reqSaveUserDetailById } from '@/api/employees'
export default {
  name: 'EmployeesDetail',
  components: {
    JobInfo,
    UserInfo
  },
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      pwdInfo: {
        username: '',
        password: ''
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '密码不可以为空', trigger: ['blur', 'change'] },
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  // 页面创建回填信息
  created() {
    this.loadUserInfo()
  },
  methods: {
    // 加载用户名 ,信息回填
    async loadUserInfo() {
      try {
        const res = await getDetailInfo(this.id)
        if (res.code === 10000) {
          this.pwdInfo.username = res.data.username
          this.pwdInfo.password = res.data.password
        }
      } catch (error) {
        this.$message.error('获取员工信息失败')
      }
    },
    // 点击更新,用户信息 ,提交
    async handleUpdate() {
      // 点击确定 ,做一个兜底校验
      this.$refs.pwdForm.validate(async valid => {
        if (!valid) return
        try {
          const res = await reqSaveUserDetailById({
            ...this.pwdInfo,
            id: this.id
          })
          if (res.code === 10000) {
            this.$message.success('更新信息成功')
          }
          // 提示一下
        } catch (error) {
          this.$message.error('更新信息失败')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .employees-detail-container {
    .el-tab-pane {
      padding-top: 10px;
    }

    .tooltip-box {
      position: absolute;
      right: 30px;
      top: 10px;
      z-index: 999;
    }
  }
</style>
